package com.liukai.novel_type.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.liukai.novel_commons.entities.NovelInfo;
import com.liukai.novel_commons.utils.Constans;
import com.liukai.novel_rack.search_interface.OnSearchItemListener;
import com.liukai.novel_type.R;

import java.util.ArrayList;
import java.util.List;

public class TypePagerRecyclerAdapter extends RecyclerView.Adapter<TypePagerRecyclerAdapter.MyTypePagerViewHolder> {

    private List<NovelInfo> mNovelInfoList = new ArrayList<>();
    private Context mContext;

    @NonNull
    @Override
    public MyTypePagerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        this.mContext = parent.getContext();
        View view = View.inflate(parent.getContext(), R.layout.item_type_pager, null);
        return new MyTypePagerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyTypePagerViewHolder holder, int position) {
        final NovelInfo novelInfo = mNovelInfoList.get(position);
        holder.mTv_novelName.setText(novelInfo.getNovelName());

        Glide.with(mContext)
                .load(Constans.IMAGE_BASE_URL + novelInfo.getNovelImg())
                .into(holder.mIv_novelImg);
//        holder.mIv_novelImg.G
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnSearchItemListener.onItemClick(novelInfo);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mNovelInfoList.size();
    }

    public void setNovelInfoList(List<NovelInfo> novelInfoList) {
        if (mNovelInfoList != null)
            mNovelInfoList.clear();
        this.mNovelInfoList.addAll(novelInfoList);
        notifyDataSetChanged();
    }

    class MyTypePagerViewHolder extends RecyclerView.ViewHolder {


        public TextView mTv_novelName;
        public ImageView mIv_novelImg;

        public MyTypePagerViewHolder(@NonNull View itemView) {
            super(itemView);
            mIv_novelImg = itemView.findViewById(R.id.novelImg);
            mTv_novelName = itemView.findViewById(R.id.novelName);
        }

    }

    private OnSearchItemListener mOnSearchItemListener;

    public void setOnSearchItemListener(OnSearchItemListener onSearchItemListener) {
        mOnSearchItemListener = onSearchItemListener;
    }
}
